package com.example.qqlaobing.Umoney.view.fragment.home.act;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qqlaobing.Umoney.R;
import com.example.qqlaobing.Umoney.been.PostBean;
import com.example.qqlaobing.Umoney.net.NetRequestBusinessDefine;
import com.example.qqlaobing.Umoney.net.NetRequestRULBuilder;
import com.example.qqlaobing.Umoney.presenter.presenterImpl.OrderInfoPresenterImpl;
import com.example.qqlaobing.Umoney.view.activity.MainActivity;
import com.example.qqlaobing.Umoney.view.fragment.home.act.adapter.LoanRecognitionAdapter;
import com.example.qqlaobing.Umoney.view.fragment.home.act.bean.LoanRecognitionBean;
import com.example.qqlaobing.Umoney.view.myview.LoadingDialog;
import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 首页=》贷款确认
 */
public class Act_LoanRecognition extends AppCompatActivity {

    @BindView(R.id.damount)
    TextView damount;
    @BindView(R.id.damount1)
    TextView damount1;
    @BindView(R.id.daozhang)
    TextView daozhang;
    @BindView(R.id.qs)
    TextView qs;
    @BindView(R.id.qs1)
    TextView qs1;
    @BindView(R.id.Interest)
    TextView Interest;
    @BindView(R.id.Interest1)
    TextView Interest1;
    @BindView(R.id.btn_loan)
    Button btnLoan;
    @BindView(R.id.tv_exit)
    ImageView tvExit;
    @BindView(R.id.myReyclerView)
    RecyclerView myReyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.act_loanrecognition);
        StatusBarCompat.setStatusBarColor(this,getResources().getColor(R.color.title_bar));
        ButterKnife.bind(this);
        initView();
        initData();
    }

    List<LoanRecognitionBean.DataBean.LoanBean.FqListBean> datas = new ArrayList<>();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null == msg) {
                return;
            }
            switch (msg.what) {
                case NetRequestBusinessDefine.K_get:
                    datas.clear();
                    JSONObject response = (JSONObject) msg.obj;
                    Gson gson = new Gson();
                    LoanRecognitionBean bean = gson.fromJson(response.toString(), LoanRecognitionBean.class);
                    if (bean.getStatus() == 1) {
                        damount.setText(bean.getData().getLoan().getDamount() + "元");
                        damount1.setText(bean.getData().getLoan().getDamount() + "元");
                        daozhang.setText(bean.getData().getLoan().getDaozhang() + "元");
                        qs.setText(bean.getData().getLoan().getDays() + "天");
                        Interest.setText(bean.getData().getLoan().getInterest() + "元");
                        Interest1.setText(bean.getData().getLoan().getService_fee() + "元");
                        qs1.setText(bean.getData().getLoan().getQs() + "期");
                        for (int i = 0; i < bean.getData().getLoan().getFq_list().size(); i++) {
                            if (i < 4) {
                                datas.add(bean.getData().getLoan().getFq_list().get(i));
                            }

                        }
                        LoanRecognitionAdapter adapter = new LoanRecognitionAdapter(datas);
                        myReyclerView.setAdapter(adapter);

                    }
                    break;
                case NetRequestBusinessDefine.K_POST_APPLY:
                    dialog.close();
                    try {
                        if (msg.arg1 == 404) {
                            return;
                        }
                        Log.e("111", "main1");
                        JSONObject response1 = (JSONObject) msg.obj;
                        String resultCode = response1.getString("status");
                        if (resultCode.equals("1")) {
                            Toast.makeText(Act_LoanRecognition.this, "贷款提交成功", Toast.LENGTH_LONG).show();
                            Act_LoanRecognition.this.finish();
                        } else {
                            Toast.makeText(Act_LoanRecognition.this, response1.getString("msg"), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    public void initView() {
        myReyclerView.setLayoutManager(new LinearLayoutManager(this));


    }

    public void initData() {

        OrderInfoPresenterImpl orderInfoPresenter = new OrderInfoPresenterImpl(mHandler, NetRequestBusinessDefine.oderno);
        orderInfoPresenter.postData();
    }


    @OnClick({R.id.btn_loan, R.id.tv_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_loan:
                postselectProducts();
                break;
            case R.id.tv_exit:
                finish();
                break;
        }
    }

    private LoadingDialog dialog;

    /**
     * 提交数据
     */
    public void postselectProducts() {
        dialog = new LoadingDialog(Act_LoanRecognition.this, "上传中...");
        dialog.show();
        OkHttpUtils
                .post()
                .url(NetRequestRULBuilder.buildRequestUrl(NetRequestBusinessDefine.K_POST_APPLY))
                .addHeader("token", NetRequestBusinessDefine.K_TOKEN)
                .addParams("orderno", NetRequestBusinessDefine.oderid)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        dialog.close();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        dialog.close();
                        PostBean appLyLoan = gson.fromJson(response, PostBean.class);
                        if (appLyLoan.getStatus() == 1) {
                            Intent intent = new Intent(Act_LoanRecognition.this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            Toast.makeText(getApplicationContext(), appLyLoan.getMsg(), Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), appLyLoan.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }

}
